package org.example.backend.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;
import java.nio.file.Paths;

/**
 * Web MVC配置类
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${file.access-path}")
    private String accessPath;

    /**
     * 配置静态资源映射
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 动态获取项目路径
        String projectDir = System.getProperty("user.dir");
        // 构建上传文件夹的绝对路径
        String uploadDir = Paths.get(projectDir,  "src", "main", "resources", "upload").toString();

       
        // 配置上传文件访问路径，不需要认证
        registry.addResourceHandler(accessPath)
                .addResourceLocations("file:" + uploadDir + "/");

    }
}
